<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>使用 isOldIE() 方法</title>
</head>
<body>
<img id="img-0" src="usa.gif" alt="usa">
<div id="status"></div>
<script>
    var evt = {
        addListener: function (obj, type, fn) {
            if (obj.addEventListener) {
                obj.addEventListener(type, fn);
            } else {
                obj.attachEvent('on' + type, fn);
            }
        },

        removeListener: function (obj, type, fn) {
            if (obj.removeEventListener) {
                obj.removeEventListener(type, fn);
            } else {
                obj.detachEvent('on' + type, fn);
            }
        },

        getTarget: function (e) {
            if (e.target) {
                return e.target;
            }
            return e.srcElement;
        },

        preventDefault: function (e) {
            if (e.preventDefault) {
                e.preventDefault();
            } else {
                e.returnValue = false;
            }
        },

        isOldIE: function () {
            return typeof document.addEventListener == 'undefined';
        }
    };

    var myImages = [
        'usa.gif',
        'canada.gif',
        'jamaica.gif',
        'mexico.gif'
    ];

    function changeImg(e) {
        var target = evt.getTarget(e);
        var newImgNumber;
        do {
            newImgNumber = Math.round(Math.random() * 3);
        } while (target.src.indexOf(myImages[newImgNumber]) != -1);
        target.src = myImages[newImgNumber];
        target.alt = myImages[newImgNumber];
    }

    function updateStatus(e) {
        var target = evt.getTarget(e);
        var status = document.getElementById('status');
        status.innerHTML = 'The iamge changed to ' + target.src;
        if (target.indexOf('mexico') != -1) {
            evt.removeListener('onclick', changeImg);
            evt.removeListener('onclick', updateStatus);
        }
    }

    var imgObj = document.getElementById('img-0');
    if (evt.isOldIE()) {
        evt.addListener(imgObj, 'click', updateStatus);
        evt.addListener(imgObj, 'click', changeImg);
    }else {
        evt.addListener(imgObj, 'click', changeImg);
        evt.addListener(imgObj, 'click', updateStatus);
    }
</script>
</body>
</html>